package com.thb.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thb.business.constant.ArticleType;
import com.thb.business.model.Article;
import com.thb.business.model.ArticlePara;
import com.thb.business.service.IArticleService;
import com.thb.utils.ResponseTool;

@Controller
public class ArticleController  {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IArticleService articleService;
	
	@RequestMapping(value = "/article/list" , method = RequestMethod.POST)
	@ResponseBody
	public Object articleList(HttpServletRequest request){
		String json = request.getParameter("args");
		String sid = request.getParameter("sid");
		logger.info(String.format("args is: %s,sid is : %s", json,sid));
		ArticlePara para = JSON.parseObject(json, ArticlePara.class);
		List<Article> cList = articleService.findByPara(para);
		int count = articleService.countByPara(para);
		List<Article> hList = new ArrayList<Article>();
		if(para.getColumnId() == 1){
			//homepage
			ArticlePara para1 = new ArticlePara();
			para1.setColumnId(para.getColumnId());
			para1.setType(ArticleType.datu);
			para1.setPageSize(4);
			hList = articleService.findByPara(para);
		}
		JSONObject page = new JSONObject();
		page.put("total", count);
		JSONObject r = new JSONObject();
		r.put("aList", cList);
		r.put("page",page);
		r.put("hList", hList);
		
		return ResponseTool.returns(r);
	}
	
	@RequestMapping(value = "/article/specialList" , method = RequestMethod.POST)
	@ResponseBody
	public Object articleSpecialList(HttpServletRequest request){
		String json = request.getParameter("args");
		String sid = request.getParameter("sid");
		logger.info(String.format("args is: %s,sid is : %s", json,sid));
		ArticlePara para = JSON.parseObject(json, ArticlePara.class);
		JSONObject page = new JSONObject();
		
		if(para.getLabel() == null || "".equals(para.getLabel())){
			List<Article> aList = articleService.findSpecialByPara(para);
			page.put("aList", aList);
		}else{
			HashMap<String, List<Article>> aMap = articleService.findSpecialAllByPara(para);
			page.put("aList", aMap);
		}
		
		return ResponseTool.returns(page);
	}

}
